package org.example.pd.controller;

import cn.hutool.json.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.example.cof.SearchScoreRequest;
import org.example.pd.domain.*;
import org.example.pd.service.MajorSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

@Slf4j
@Controller
@RequestMapping()
public class MajorSourceController {
    @Autowired
    MajorSourceService majorSourceService;

    static String year = "2023";

    @CrossOrigin(origins = "http://localhost:5173")
    @PostMapping("/majorSource/searchScore")
    @ResponseBody
    public JSONObject[] searchScore(@RequestBody SearchScoreRequest request) {
        System.out.println("参数: " + Arrays.deepToString(request.getCanshu()));
        String[][] parameter = request.getCanshu();
        String subject = parameter[0][0];
        String majorName = parameter[0][1];

//  [i][0]存的是subject，[i][1]是majorName [i][2]存的是schoolCode
        List<String> schoolCode = new ArrayList<>();
        for (int n = 0; n < parameter.length; n++) {
            schoolCode.add(parameter[n][2]);
//            System.out.println("第" +n+ "个schoolCode:"+ canshu[n][2]);
        }
        System.out.println("-----------");
        for (int n = 0; n < parameter.length; n++) {
            System.out.print("\"" + parameter[n][2] + "\",");
        }

        List<String> schoolNames = majorSourceService.geySchoolNameByCode(schoolCode, year);
        System.out.println("schoolNames:" + schoolNames);

        JSONObject[] result = new JSONObject[schoolCode.size()]; // 存储结果的JSONObject数组
        for (int i = 0; i < schoolCode.size(); i++) {
            result[i] = new JSONObject(); // 创建JSONObject对象
            result[i].put("school", schoolNames.get(i)); // 设置学校名称
            result[i].put("major", majorName);
        }


//        result = admissionScore(result, year, schoolCode, majorName);
//        result = firstVolunteer(result, year, schoolCode, majorName);
//        result = admissionRate(result, year, schoolCode, majorName);
//        result = advantageousMajors(result, year, schoolCode, majorName);
//        result = transferMajors(result, year, schoolCode, majorName);
//        result = majorSatisfaction(result, year, schoolCode, majorName);
//        result = highLevelTitle(result, year, schoolCode, majorName);
//        result = highLevelTeam(result, year, schoolCode, majorName);
//        result = numberStructure(result, year, schoolCode, majorName);
//        result = getTeachingAward(result, year, schoolCode, majorName);
//        result = researchLevel(result, year, schoolCode, majorName);
        result = getLaboratoryCampus(result, year, schoolCode, majorName);
//        result = trainingBase(result, year, schoolCode, majorName);
//        result = getTopCourse(result, year, schoolCode, majorName);
//        result = publishTextbooks(result, year, schoolCode, majorName);
//        result = informationCourse(result, year, schoolCode, majorName,subject);
//        result = participation(result, year, schoolCode, majorName);
//        result = learnEffective(result, year, schoolCode, majorName);
//        result = graduateTrain(result, year, schoolCode, majorName);
//        result = employRate(result, year, schoolCode, majorName);
//        result = higherSchoolRate(result, year, schoolCode, majorName);
//        result = EmployDestination(result, year, schoolCode, majorName);




        result = teachingSatisfaction(result, year, schoolCode, majorName);
        result = professionalCompetence(result, year, schoolCode, majorName);
        result = generalAbility(result, year, schoolCode, majorName);
        result = passRate(result, year, schoolCode, majorName);
        result = professionalRelevance(result, year, schoolCode, majorName);

        System.out.println("result:" + Arrays.toString(result));
        return result;
    }

    // 录取分数
    public JSONObject[] admissionScore(JSONObject[] result, String year, List<String> schoolCode, String majorName) {
        List<AdmissionScore> admissionScores = majorSourceService.getAdmissionScore(year, schoolCode, majorName);
        // 存储每所学校及其对应的总分（文科+理科）和招生省份数量
        Map<String, Double> schoolTotalScores = new HashMap<>();
        Map<String, Integer> schoolProvinceCounts = new HashMap<>();
        // 如果没有数据，直接将所有学校的lqfs设置为"-"
        if (admissionScores.isEmpty()) {
            for (int i = 0; i < schoolCode.size(); i++) {
                result[i].put("lqfs", "-");
            }
            return result;
        }

        // 遍历admissionScores，计算每所学校的总分和省份数量
        for (AdmissionScore score : admissionScores) {
            String schoolName = score.getSchoolName();
            double totalScore = score.getLiteratureSubject() + score.getScienceSubject();
            schoolTotalScores.put(schoolName, schoolTotalScores.getOrDefault(schoolName, 0.0) + totalScore);
            schoolProvinceCounts.put(schoolName, schoolProvinceCounts.getOrDefault(schoolName, 0) + 1);
        }

        // 计算每所学校的平均分
        Map<String, Double> schoolAverageScores = new HashMap<>();
        for (String schoolName : schoolTotalScores.keySet()) {
            double totalScore = schoolTotalScores.get(schoolName);
            int provinceCount = schoolProvinceCounts.get(schoolName);
            double averageScore = totalScore / provinceCount;
            schoolAverageScores.put(schoolName, averageScore);
        }

        // 归一化处理
        double minScore = Collections.min(schoolAverageScores.values());
        double maxScore = Collections.max(schoolAverageScores.values());
        // 如果最大值和最小值相等，说明只有一个数值，无法做归一化处理
        if (minScore == maxScore) {
            for (int i = 0; i < schoolCode.size(); i++) {
                JSONObject schoolObj = result[i];
                if (schoolAverageScores.containsKey(schoolObj.get("school"))) {
                    schoolObj.put("lqfs", 1);
                } else {
                    schoolObj.put("lqfs", "-");
                }
            }
        } else {
            for (int i = 0; i < schoolCode.size(); i++) {
                JSONObject schoolObj = result[i];
                String schoolName = (String) schoolObj.get("school");
                if (schoolAverageScores.containsKey(schoolName)) {
                    double originalScore = schoolAverageScores.get(schoolName);
                    // 归一化公式：(x - min) / (max - min)
                    double normalizedScore = Math.round((originalScore - minScore) / (maxScore - minScore) * 10000.0) / 10000.0;
                    schoolObj.put("lqfs", normalizedScore);
                } else {
                    schoolObj.put("lqfs", "-");
                }
            }
        }
        return result;
    }

    // 第一志愿率
    public JSONObject[] firstVolunteer(JSONObject[] result, String year, List<String> schoolCode, String majorName) {
        List<FirstVolunteer> firstVolunteers = majorSourceService.getFirstVolunteer(year, schoolCode, majorName);
        Map<String, Double> firstVolunteerRates = new HashMap<>();
        for (FirstVolunteer firstVolunteer : firstVolunteers) {
            String schoolName = firstVolunteer.getSchoolName();
            double firstRate = firstVolunteer.getFirstWish() / firstVolunteer.getAdmissions();
            firstVolunteerRates.put(schoolName, firstVolunteerRates.getOrDefault(schoolName, 0.0) + firstRate);
        }
        for (int i = 0; i < schoolCode.size(); i++) {
            JSONObject schoolObj = result[i];
            String schoolName = (String) schoolObj.get("school");
            if (firstVolunteerRates.containsKey(schoolName)) {
                double firstRate = firstVolunteerRates.get(schoolName);
                schoolObj.put("dyzyl", Math.round(firstRate * 10000.0) / 10000.0);
            } else {
                schoolObj.put("dyzyl", "-");
            }
        }
        return result;
    }

    // 报道率
    public JSONObject[] admissionRate(JSONObject[] result, String year, List<String> schoolCode, String majorName) {
        List<FirstVolunteer> admissionRates = majorSourceService.getAdmissionRate(year, schoolCode, majorName);
        Map<String, Double> admissionRateRates = new HashMap<>();
        for (FirstVolunteer firstVolunteer : admissionRates) {
            String schoolName = firstVolunteer.getSchoolName();

            BigDecimal firstWish = BigDecimal.valueOf(firstVolunteer.getFirstWish());
            BigDecimal admissions = BigDecimal.valueOf(firstVolunteer.getAdmissions());
            if (admissions.compareTo(BigDecimal.ZERO) == 0) {
                admissionRateRates.put(schoolName, 0.0);
            } else {
                double rate = firstWish.divide(admissions, 4, RoundingMode.HALF_UP).doubleValue();
                admissionRateRates.put(schoolName, rate);
            }
        }
        for (int i = 0; i < schoolCode.size(); i++) {
            JSONObject schoolObj = result[i];
            String schoolName = (String) schoolObj.get("school");
            if (admissionRateRates.containsKey(schoolName)) {
                double firstRate = admissionRateRates.get(schoolName);
                schoolObj.put("bdl", firstRate);
            } else {
                schoolObj.put("bdl", "-");
            }
        }
        return result;
    }

    // 优势专业
    public JSONObject[] advantageousMajors(JSONObject[] result, String year, List<String> schoolCode, String majorName) {
        List<AdvantageousMajors> advantageousMajors = majorSourceService.getAdvantageousMajors(year, schoolCode, majorName);
        // 存储每所学校对应专业的优势专业得分总和
        Map<String, Double> schoolTotalScores = new HashMap<>();

        // 如果没有数据，直接将所有学校的yszylx设置为"-"
        if (advantageousMajors.isEmpty()) {
            for (int i = 0; i < schoolCode.size(); i++) {
                result[i].put("yszy", "-");
            }
            return result;
        }

        // 遍历advantageousMajors，计算每所学校的特定专业的优势专业得分总和
        for (AdvantageousMajors am : advantageousMajors) {
            String schoolName = am.getSchoolName();
            double score = am.getScore();
            // 累加得分至对应的学校
            schoolTotalScores.put(schoolName, schoolTotalScores.getOrDefault(schoolName, 0.0) + score);
        }

        // 更新result数组中的相应字段
        for (int i = 0; i < schoolCode.size(); i++) {
            String schoolName = (String) result[i].get("school");
            // 如果该学校存在于schoolTotalScores中，则更新其分数；否则设置为"-"
            if (schoolTotalScores.containsKey(schoolName) && schoolTotalScores.get(schoolName) != 0) {
                result[i].put("yszy", schoolTotalScores.get(schoolName));
            } else {
                result[i].put("yszy", "-");
            }
        }
        return result;
    }

    // 转专业情况
    public JSONObject[] transferMajors(JSONObject[] result, String year, List<String> schoolCode, String majorName) {
        List<TransferMajor> transferMajors = majorSourceService.transferMajor(year, schoolCode, majorName);
        // 如果没有数据，直接将所有学校的yszylx设置为"-"
        if (transferMajors.isEmpty()) {
            for (int i = 0; i < schoolCode.size(); i++) {
                result[i].put("zzyqk", "-");
            }
            return result;
        }
        double minDifference = Double.MAX_VALUE;
        double maxDifference = Double.MIN_VALUE;
        for (TransferMajor transferMajor : transferMajors) {
            double difference = transferMajor.getDifference();
            minDifference = Math.min(minDifference, difference);
            maxDifference = Math.max(maxDifference, difference);
        }

        minDifference = Math.abs(minDifference);
        maxDifference = maxDifference + minDifference;
        for (TransferMajor transferMajor : transferMajors) {
            transferMajor.setDifference(transferMajor.getDifference() + minDifference);
        }

        Map<String, Double> transferMajorsScore = new HashMap<>();
        for (TransferMajor transferMajor : transferMajors) {
            transferMajorsScore.put(transferMajor.getSchoolName(), transferMajor.getDifference());
        }


        // 如果只有一个学校
        if (0.0 == maxDifference) {
            for (int i = 0; i < schoolCode.size(); i++) {
                JSONObject schoolObj = result[i];
                if (transferMajorsScore.containsKey(schoolObj.get("school"))) {
                    schoolObj.put("zzyqk", 1);
                } else {
                    schoolObj.put("zzyqk", "-");
                }
            }
        } else {
            for (int i = 0; i < schoolCode.size(); i++) {
                String schoolName = (String) result[i].get("school");
                if (transferMajorsScore.containsKey(schoolName)) {
                    double difference = transferMajorsScore.get(schoolName);
                    // 归一化公式：(x - min) / (max - min)
                    double score = Math.round(difference / maxDifference * 10000.0) / 10000.0;
                    result[i].put("zzyqk", score);
                } else {
                    result[i].put("zzyqk", "-");
                }
            }
        }
        return result;
    }

    // 专业满意度
    public JSONObject[] majorSatisfaction(JSONObject[] result, String year, List<String> schoolCode, String majorName) {
        for (int i = 0; i < schoolCode.size(); i++) {
            result[i].put("zymyd", "问卷数据");
        }

        return result;
    }

    // 高层次称号gccch
    public JSONObject[] highLevelTitle(JSONObject[] result, String year, List<String> schoolCode, String majorName) {
        List<HighLevelTitle> highLevelTitle = majorSourceService.getHighLevelTitle(year, schoolCode, majorName);
        if (highLevelTitle.isEmpty()) {
            for (int i = 0; i < schoolCode.size(); i++) {
                result[i].put("gccch", "-");
            }
            return result;
        }
        Map<String, Double> highLevelTitleScore = new HashMap<>(); // 存放学校和分数
        for (HighLevelTitle hlt : highLevelTitle) {
            highLevelTitleScore.put(hlt.getSchoolName(), highLevelTitleScore.getOrDefault(hlt.getSchoolName(), 0.0) + hlt.getScore());
        }
        double minScore = Collections.min(highLevelTitleScore.values());
        double maxScore = Collections.max(highLevelTitleScore.values());
        if (minScore == maxScore) {
            for (int i = 0; i < schoolCode.size(); i++) {
                JSONObject schoolObj = result[i];
                if (highLevelTitleScore.containsKey(schoolObj.get("school"))) {
                    schoolObj.put("gccch", 1);
                } else {
                    schoolObj.put("gccch", "-");
                }
            }
        } else {
            result = jsonPut(result, schoolCode, highLevelTitleScore, "gccch");
        }
        return result;
    }

    // 高层次团队
    public JSONObject[] highLevelTeam(JSONObject[] result, String year, List<String> schoolCode, String majorName) {
        List<HighLevelTeam> HighLevelTeam = majorSourceService.getHighLevelTeam(year, schoolCode, majorName);
        if (HighLevelTeam.isEmpty()) {
            for (int i = 0; i < schoolCode.size(); i++) {
                result[i].put("gcctd", "-");
            }
            return result;
        }
        Map<String, Double> highLevelTeamScore = new HashMap<>(); // 存放学校和分数
        for (HighLevelTeam hlt : HighLevelTeam) {
            highLevelTeamScore.put(hlt.getSchoolName(), highLevelTeamScore.getOrDefault(hlt.getSchoolName(), 0.0) + hlt.getScore());
        }
        double minScore = Collections.min(highLevelTeamScore.values());
        double maxScore = Collections.max(highLevelTeamScore.values());
        if (minScore == maxScore) {
            for (int i = 0; i < schoolCode.size(); i++) {
                JSONObject schoolObj = result[i];
                if (highLevelTeamScore.containsKey(schoolObj.get("school"))) {
                    schoolObj.put("gcctd", 1);
                } else {
                    schoolObj.put("gcctd", "-");
                }
            }
        } else {
            result = jsonPut(result, schoolCode, highLevelTeamScore, "gcctd");
        }
        return result;
    }

    //数量结构
    public JSONObject[] numberStructure(JSONObject[] result, String year, List<String> schoolCode, String majorName) {
        //数量结构-专任教师数量及结构
        List<FullTimeTeachers> fullTimeTeachers = majorSourceService.numberStructure(year, schoolCode, majorName);
        //数量结构-专业教师承担教学情况
        List<FullTimeTeachersTeach> fullTimeTeachersTeaches = majorSourceService.getFullTimeTeachersTeach(year, schoolCode, majorName);
        // 如果没有任何学校的数据，直接将所有学校的sljg设置为"-"
        if (fullTimeTeachers.isEmpty() && fullTimeTeachersTeaches.isEmpty()) {
            for (int i = 0; i < schoolCode.size(); i++) {
                result[i].put("sljg", "-");
            }
            return result;
        }

        // 存储每所学校在fullTimeTeachers各学校归一化的值
        Map<String, Double> schoolScoresFTT = new HashMap<>();
        // 存储每所学校在fullTimeTeachersTeaches各学校归一化的值
        Map<String, Double> schoolScoresFTTT = new HashMap<>();

        // 初始化最大值和最小值数组
        double[] FTTmaxValues = new double[10];
        double[] FTTminValues = new double[10];
        for (int i = 0; i < 10; i++) {
            FTTmaxValues[i] = Double.MIN_VALUE;
            FTTminValues[i] = Double.MAX_VALUE;
        }
// 遍历列表，更新最大值和最小值
        for (FullTimeTeachers teacher : fullTimeTeachers) {
            double[] values = {
                    teacher.getFullTimeTeachersNumber(),
                    teacher.getHighTitleNumber(),
                    teacher.getHighTitleRatio(),
                    teacher.getProfessorsNumber(),
                    teacher.getProfessorsRatio(),
                    teacher.getPostgraduateNumber(),
                    teacher.getPostgraduateRatio(),
                    teacher.getNewTeacherFiveYears(),
                    teacher.getTowTeacher(),
                    teacher.getBackground()
            };
            for (int i = 0; i < values.length; i++) {
                if (values[i] > FTTmaxValues[i]) {
                    FTTmaxValues[i] = values[i];
                }
                if (values[i] < FTTminValues[i]) {
                    FTTminValues[i] = values[i];
                }
            }
        }
        // 需要判断单个学校数据，无法归一化处理的情况，也就是max == min
        for (FullTimeTeachers teacher : fullTimeTeachers) {
            teacher.setFullTimeTeachersNumber((FTTmaxValues[0] == FTTminValues[0] ? 1.0:(teacher.getFullTimeTeachersNumber() - FTTminValues[0]) / (FTTmaxValues[0] - FTTminValues[0])));
            teacher.setHighTitleNumber((FTTmaxValues[1] == FTTminValues[1] ? 1.0:(teacher.getHighTitleNumber() - FTTminValues[1]) / (FTTmaxValues[1] - FTTminValues[1])));
            teacher.setHighTitleRatio((FTTmaxValues[2] == FTTminValues[2] ? 1.0:(teacher.getHighTitleRatio() - FTTminValues[2]) / (FTTmaxValues[2] - FTTminValues[2])));
            teacher.setProfessorsNumber((FTTmaxValues[3] == FTTminValues[3] ? 1.0:(teacher.getProfessorsNumber() - FTTminValues[3]) / (FTTmaxValues[3] - FTTminValues[3])));
            teacher.setProfessorsRatio((FTTmaxValues[4] == FTTminValues[4] ? 1.0:(teacher.getProfessorsRatio() - FTTminValues[4]) / (FTTmaxValues[4] - FTTminValues[4])));
            teacher.setPostgraduateNumber((FTTmaxValues[5] == FTTminValues[5] ? 1.0:(teacher.getPostgraduateNumber() - FTTminValues[5]) / (FTTmaxValues[5] - FTTminValues[5])));
            teacher.setPostgraduateRatio((FTTmaxValues[6] == FTTminValues[6] ? 1.0:(teacher.getPostgraduateRatio() - FTTminValues[6]) / (FTTmaxValues[6] - FTTminValues[6])));
            teacher.setNewTeacherFiveYears((FTTmaxValues[7] == FTTminValues[7] ? 1.0:(teacher.getNewTeacherFiveYears() - FTTminValues[7]) / (FTTmaxValues[7] - FTTminValues[7])));
            teacher.setTowTeacher((FTTmaxValues[8] == FTTminValues[8] ? 1.0:(teacher.getTowTeacher() - FTTminValues[8]) / (FTTmaxValues[8] - FTTminValues[8])));
            teacher.setBackground((FTTmaxValues[9] == FTTminValues[9] ? 1.0:(teacher.getBackground() - FTTminValues[9]) / (FTTmaxValues[9] - FTTminValues[9])));
            double score = (teacher.getFullTimeTeachersNumber() + teacher.getHighTitleNumber() + teacher.getHighTitleRatio() + teacher.getProfessorsNumber()
                    + teacher.getProfessorsRatio() + teacher.getPostgraduateNumber() + teacher.getPostgraduateRatio() + teacher.getNewTeacherFiveYears()
                    + teacher.getTowTeacher() + teacher.getBackground()) / 10;
            schoolScoresFTT.put(teacher.getSchoolName(), score);
        }

        double[] FTTTmaxValues = new double[9];
        double[] FTTTminValues = new double[9];
        for (int i = 0; i < 9; i++) {
            FTTTmaxValues[i] = Double.MIN_VALUE;
            FTTTminValues[i] = Double.MAX_VALUE;
        }
        for (FullTimeTeachersTeach teacher : fullTimeTeachersTeaches) {
            double[] values = {
                    teacher.getMajorCoursesNumber(),
                    teacher.getMajorTeachersTeachNumber(),
                    teacher.getMajorTeachersTeachRatio(),
                    teacher.getSeniorTitleCourseCount(),
                    teacher.getSeniorTitleCourseRatio(),
                    teacher.getProfessorCourseCount(),
                    teacher.getProfessorCourseRatio(),
                    teacher.getAssociateProfessorCourseCount(),
                    teacher.getAssociateProfessorCourseRatio()
            };
            for (int i = 0; i < values.length; i++) {
                if (values[i] > FTTTmaxValues[i]) {
                    FTTTmaxValues[i] = values[i];
                }
                if (values[i] < FTTTminValues[i]) {
                    FTTTminValues[i] = values[i];
                }
            }
        }
        for (FullTimeTeachersTeach teacher : fullTimeTeachersTeaches) {
            teacher.setMajorCoursesNumber(FTTTmaxValues[0] == FTTTminValues[0] ? 1.0: (teacher.getMajorCoursesNumber() - FTTTminValues[0]) / (FTTTmaxValues[0] - FTTTminValues[0]));
            teacher.setMajorTeachersTeachNumber(FTTTmaxValues[1] == FTTTminValues[1] ? 1.0: (teacher.getMajorTeachersTeachNumber() - FTTTminValues[1]) / (FTTTmaxValues[1] - FTTTminValues[1]));
            teacher.setMajorTeachersTeachRatio(FTTTmaxValues[2] == FTTTminValues[2] ? 1.0: (teacher.getMajorTeachersTeachRatio() - FTTTminValues[2]) / (FTTTmaxValues[2] - FTTTminValues[2]));
            teacher.setSeniorTitleCourseCount(FTTTmaxValues[3] == FTTTminValues[3] ? 1.0: (teacher.getSeniorTitleCourseCount() - FTTTminValues[3]) / (FTTTmaxValues[3] - FTTTminValues[3]));
            teacher.setSeniorTitleCourseRatio(FTTTmaxValues[4] == FTTTminValues[4] ? 1.0: (teacher.getSeniorTitleCourseRatio() - FTTTminValues[4]) / (FTTTmaxValues[4] - FTTTminValues[4]));
            teacher.setProfessorCourseCount(FTTTmaxValues[5] == FTTTminValues[5] ? 1.0: (teacher.getProfessorCourseCount() - FTTTminValues[5]) / (FTTTmaxValues[5] - FTTTminValues[5]));
            teacher.setProfessorCourseRatio(FTTTmaxValues[6] == FTTTminValues[6] ? 1.0: (teacher.getProfessorCourseRatio() - FTTTminValues[6]) / (FTTTmaxValues[6] - FTTTminValues[6]));
            teacher.setAssociateProfessorCourseCount(FTTTmaxValues[7] == FTTTminValues[7] ? 1.0: (teacher.getAssociateProfessorCourseCount() - FTTTminValues[7]) / (FTTTmaxValues[7] - FTTTminValues[7]));
            teacher.setAssociateProfessorCourseRatio(FTTTmaxValues[8] == FTTTminValues[8] ? 1.0: (teacher.getAssociateProfessorCourseRatio() - FTTTminValues[8]) / (FTTTmaxValues[8] - FTTTminValues[8]));
            double score = (teacher.getMajorCoursesNumber() + teacher.getMajorTeachersTeachNumber() + teacher.getMajorTeachersTeachRatio() + teacher.getSeniorTitleCourseCount()
                    + teacher.getSeniorTitleCourseRatio() + teacher.getProfessorCourseCount() + teacher.getProfessorCourseRatio() + teacher.getAssociateProfessorCourseCount()
                    + teacher.getAssociateProfessorCourseRatio()) / 9;
            schoolScoresFTTT.put(teacher.getSchoolName(), score);
        }
        // 存储学校的总得分,先将专任教师数量及结构的分数都放进去
        Map<String, Double> schoolScores = new HashMap<>(schoolScoresFTT);
        for (Map.Entry<String, Double> entry : schoolScoresFTTT.entrySet()) {
            String schoolName = entry.getKey();
            Double scoreB = entry.getValue();

            if (schoolScores.containsKey(schoolName)) {
                // 某个学校有专任教师数量及结构、专业教师承担教学情况，求和后除以2
                Double scoreA = schoolScores.get(schoolName);
                Double averageScore = (scoreA + scoreB) / 2.0;
                schoolScores.put(schoolName, averageScore);
            } else {
                // 某个学校仅有专任教师数量及结构
                schoolScores.put(schoolName, scoreB);
            }
        }

        result = jsonPut(result, schoolCode, schoolScores, "sljg");
        return result;
    }

    // 教学水平
    public JSONObject[] getTeachingAward(JSONObject[] result, String year, List<String> schoolCode, String majorName) {
        //  培训进修情况
        List<TeacherTraining> trainings = majorSourceService.getTraining(year, schoolCode, majorName);
        // 教育教学研究与改革项目
        List<EduResearchReform> eduResearchReforms = majorSourceService.getEduResearchReform(year, schoolCode, majorName);
        //教学水平-教学成果奖
        List<TeachingAward> teachingAwards = majorSourceService.getTeachingAward(year, schoolCode, majorName);
        if (trainings.isEmpty() && eduResearchReforms.isEmpty() && teachingAwards.isEmpty()) {
            for (int i = 0; i < schoolCode.size(); i++) {
                result[i].put("jxsp", "-");
            }
            return result;
        }
        Map<String, Double> scoreT = new HashMap<>();// 存储trainings各学校归一化的值
        Map<String, Double> scoreER = new HashMap<>();// 存储eduResearchReforms各学校归一化的值
        Map<String, Double> scoreTA = new HashMap<>();// 存储teachingAwards各学校归一化的值

        //  培训进修情况
        // 初始化最大值和最小值数组
        double[] TmaxValues = new double[6];
        double[] TminValues = new double[6];
        for (int i = 0; i < 6; i++) {
            TmaxValues[i] = Double.MIN_VALUE;
            TminValues[i] = Double.MAX_VALUE;
        }

        for (TeacherTraining teacher : trainings) {
            double[] values = {
                    teacher.getTrainingDomestic(),
                    teacher.getTrainingForeign(),
                    teacher.getStudyForDoctor(),
                    teacher.getStudyForMaster(),
                    teacher.getDomesticExchange(),
                    teacher.getForeignExchange()
            };
            for (int i = 0; i < values.length; i++) {
                if (values[i] > TmaxValues[i]) {
                    TmaxValues[i] = values[i];
                }
                if (values[i] < TminValues[i]) {
                    TminValues[i] = values[i];
                }
            }
        }
        for (TeacherTraining teacher : trainings) {
            teacher.setTrainingDomestic(TmaxValues[0] == TminValues[0] ? 1 : (teacher.getTrainingDomestic() - TminValues[0]) / (TmaxValues[0] - TminValues[0]));
            teacher.setTrainingForeign(TmaxValues[1] == TminValues[1] ? 1 : (teacher.getTrainingForeign() - TminValues[1]) / (TmaxValues[1] - TminValues[1]));
            teacher.setStudyForDoctor(TmaxValues[2] == TminValues[2] ? 1 : (teacher.getStudyForDoctor() - TminValues[2]) / (TmaxValues[2] - TminValues[2]));
            teacher.setStudyForMaster(TmaxValues[3] == TminValues[3] ? 1 : (teacher.getStudyForMaster() - TminValues[3]) / (TmaxValues[3] - TminValues[3]));
            teacher.setDomesticExchange(TmaxValues[4] == TminValues[4] ? 1 : (teacher.getDomesticExchange() - TminValues[4]) / (TmaxValues[4] - TminValues[4]));
            teacher.setForeignExchange(TmaxValues[5] == TminValues[5] ? 1 : (teacher.getForeignExchange() - TminValues[5]) / (TmaxValues[5] - TminValues[5]));
            double score = (teacher.getTrainingDomestic() + teacher.getTrainingForeign() + teacher.getStudyForDoctor() + teacher.getStudyForMaster()
                    + teacher.getDomesticExchange() + teacher.getForeignExchange()) / 6;
            scoreT.put(teacher.getSchoolName(), score);
        }


        // 教育教学研究与改革项目

        double[] ERmaxValues = new double[6];
        double[] ERminValues = new double[6];
        for (int i = 0; i < 6; i++) {
            ERmaxValues[i] = Double.MIN_VALUE;
            ERminValues[i] = Double.MAX_VALUE;
        }

        for (EduResearchReform eduResearchReform : eduResearchReforms) {
            double[] values = {
                    eduResearchReform.getCountryCount(),
                    eduResearchReform.getCountryFunds(),
                    eduResearchReform.getNationalTeachersCount(),
                    eduResearchReform.getProvinceCount(),
                    eduResearchReform.getProvinceFunds(),
                    eduResearchReform.getProvinceTeachersCount()
            };
            for (int i = 0; i < values.length; i++) {
                if (values[i] > ERmaxValues[i]) {
                    ERmaxValues[i] = values[i];
                }
                if (values[i] < ERminValues[i]) {
                    ERminValues[i] = values[i];
                }
            }
        }
        for (EduResearchReform eduResearchReform : eduResearchReforms) {
            eduResearchReform.setCountryCount(ERmaxValues[0] == ERminValues[0] ? 1 : (eduResearchReform.getCountryCount() - ERminValues[0]) / (ERmaxValues[0] - ERminValues[0]));
            eduResearchReform.setCountryFunds(ERmaxValues[1] == ERminValues[1] ? 1 : (eduResearchReform.getCountryFunds() - ERminValues[1]) / (ERmaxValues[1] - ERminValues[1]));
            eduResearchReform.setNationalTeachersCount(ERmaxValues[2] == ERminValues[2] ? 1 : (eduResearchReform.getNationalTeachersCount() - ERminValues[2]) / (ERmaxValues[2] - ERminValues[2]));
            eduResearchReform.setProvinceCount(ERmaxValues[3] == ERminValues[3] ? 1 : (eduResearchReform.getProvinceCount() - ERminValues[3]) / (ERmaxValues[3] - ERminValues[3]));
            eduResearchReform.setProvinceFunds(ERmaxValues[4] == ERminValues[4] ? 1 : (eduResearchReform.getProvinceFunds() - ERminValues[4]) / (ERmaxValues[4] - ERminValues[4]));
            eduResearchReform.setProvinceTeachersCount(ERmaxValues[5] == ERminValues[5] ? 1 : (eduResearchReform.getProvinceTeachersCount() - ERminValues[5]) / (ERmaxValues[5] - ERminValues[5]));
            double score = (eduResearchReform.getCountryCount() + eduResearchReform.getCountryFunds() + eduResearchReform.getNationalTeachersCount()
                    + eduResearchReform.getProvinceCount() + eduResearchReform.getProvinceFunds() + eduResearchReform.getProvinceTeachersCount()) / 6;
            scoreER.put(eduResearchReform.getSchoolCode(), score);
        }


        //教学水平-教学成果奖
        double tAScore = 0.0;
        String tAschoolNameOld = "";
        for (int i = 0; i < teachingAwards.size(); i++) {
            if (i == 0 || teachingAwards.get(i).getSchoolName().equals(tAschoolNameOld)) {
                tAScore += teachingAwards.get(i).getScore();
            } else if (!teachingAwards.get(i).getSchoolName().equals(tAschoolNameOld)) {
                scoreTA.put(tAschoolNameOld, tAScore);
                tAScore = 0.0;
            }
            tAschoolNameOld = teachingAwards.get(i).getSchoolName();
            if(i == teachingAwards.size() - 1){
                scoreTA.put(tAschoolNameOld, tAScore);
            }
            if(teachingAwards.size() == 1){
                scoreTA.put(tAschoolNameOld, tAScore);
            }
        }

        //教学成果奖的归一化处理
        // 初始化最大值和最小值
        double max = Double.MIN_VALUE;
        double min = Double.MAX_VALUE;
        // 找出 map 中的最大值和最小值
        for (double value : scoreTA.values()) {
            if (value > max) {
                max = value;
            }
            if (value < min) {
                min = value;
            }
        }
        // 处理最大值和最小值相等的情况，避免除零错误
        if (max == min) {
            for (Map.Entry<String, Double> entry : scoreTA.entrySet()) {
                scoreTA.replaceAll((k, v) -> 1.0); // 当最大值和最小值相等时，归一化后的值都设为 1.0
            }
        }else {
            // 进行归一化处理
            for (Map.Entry<String, Double> entry : scoreTA.entrySet()) {
                String key = entry.getKey();
                double value = entry.getValue();
                // 使用最小 - 最大归一化公式进行计算
                double normalizedValue = (value - min) / (max - min);
                scoreTA.put(key, normalizedValue);
            }
        }
        // 对三个数值进行合并计算求平均值
        Map<String, Double> teachLevel = new HashMap<>();
        // 收集所有学校的名称（三个map的key的并集）
        Set<String> allSchools = new HashSet<>();
        allSchools.addAll(scoreT.keySet());
        allSchools.addAll(scoreER.keySet());
        allSchools.addAll(scoreTA.keySet());
        // 遍历所有学校，计算每个学校的平均分
        for (String school : allSchools) {
            double sum = 0.0;
            if (scoreT.containsKey(school)) {
                sum += scoreT.get(school);
            }
            if (scoreER.containsKey(school)) {
                sum += scoreER.get(school);
            }
            if (scoreTA.containsKey(school)) {
                sum += scoreTA.get(school);
            }
            double average = Math.round(sum / 3 * 10000.0) / 10000.0;
            teachLevel.put(school, average);
        }

        result = jsonPut(result, schoolCode, teachLevel, "jxsp");
        return result;
    }

    //科研水平
    public JSONObject[] researchLevel(JSONObject[] result, String year, List<String> schoolCode, String majorName) {
        // 科研水平
        //科研水平-主持科研项目
        List<LeadResearchProjects> leadResearchProjects = majorSourceService.getLeadResearchProjects(year, schoolCode, majorName);
        //科研水平-发表论文情况
        List<PublishPaper> publishPaper = majorSourceService.getPublishPaper(year, schoolCode, majorName);
        //科研水平-专利专著
        List<PatentMonograph> patentMonographs = majorSourceService.getPatentMonograph(year, schoolCode, majorName);
        //科研水平-教师科研成果转化情况
        List<ResTransferStatus> resTransferStatus = majorSourceService.getResTransferStatus(year, schoolCode, majorName);
        if (leadResearchProjects.isEmpty() && publishPaper.isEmpty() && patentMonographs.isEmpty() && resTransferStatus.isEmpty()) {
            for (int i = 0; i < schoolCode.size(); i++) {
                result[i].put("kysp", "-");
            }
            return result;
        }

        // 主持科研项目
        Map<String, Double> scoreLRP = new HashMap<>();// 存储leadResearchProjects各学校归一化的值 主持科研项目
        double[] LRPmax = new double[14];
        double[] LRPmin = new double[14];
        for (int i = 0; i < 14; i++) {
            LRPmax[i] = Double.MIN_VALUE;
            LRPmin[i] = Double.MAX_VALUE;
        }
        for (LeadResearchProjects lrp : leadResearchProjects) {
            double[] values = {
                    lrp.getHorizontalCount(), lrp.getHorizontalFunds(), lrp.getPortraitCountryCount(),
                    lrp.getPortraitCountryFunds(), lrp.getPortraitArmyCount(), lrp.getPortraitArmyFunds(),
                    lrp.getPortraitOverseasCount(), lrp.getPortraitOverseasFunds(), lrp.getPortraitMinisterialCount(),
                    lrp.getPortraitMinisterialFunds(), lrp.getPortraitProvinceCount(), lrp.getPortraitProvinceFunds(),
                    lrp.getPortraitProvinceOtherCount(), lrp.getPortraitProvinceOtherFunds()
            };
            for (int i = 0; i < values.length; i++) {
                if (values[i] > LRPmax[i]) {
                    LRPmax[i] = values[i];
                }
                if (values[i] < LRPmin[i]) {
                    LRPmin[i] = values[i];
                }
            }
        }
        for (LeadResearchProjects lrp : leadResearchProjects) {
            lrp.setHorizontalCount(LRPmax[0] == LRPmin[0] ? 1.0 : ((lrp.getHorizontalCount() - LRPmin[0]) / (LRPmax[0] - LRPmin[0])));
            lrp.setHorizontalFunds(LRPmax[1] == LRPmin[1] ? 1.0 : ((lrp.getHorizontalFunds() - LRPmin[1]) / (LRPmax[1] - LRPmin[1])));
            lrp.setPortraitCountryCount(LRPmax[2] == LRPmin[2] ? 1.0 : ((lrp.getPortraitCountryCount() - LRPmin[2]) / (LRPmax[2] - LRPmin[2])));
            lrp.setPortraitCountryFunds(LRPmax[3] == LRPmin[3] ? 1.0 : ((lrp.getPortraitCountryFunds() - LRPmin[3]) / (LRPmax[3] - LRPmin[3])));
            lrp.setPortraitArmyCount(LRPmax[4] == LRPmin[4] ? 1.0 : ((lrp.getPortraitArmyCount() - LRPmin[4]) / (LRPmax[4] - LRPmin[4])));
            lrp.setPortraitArmyFunds(LRPmax[5] == LRPmin[5] ? 1.0 : ((lrp.getPortraitArmyFunds() - LRPmin[5]) / (LRPmax[5] - LRPmin[5])));
            lrp.setPortraitOverseasCount(LRPmax[6] == LRPmin[6] ? 1.0 : ((lrp.getPortraitOverseasCount() - LRPmin[6]) / (LRPmax[6] - LRPmin[6])));
            lrp.setPortraitOverseasFunds(LRPmax[7] == LRPmin[7] ? 1.0 : ((lrp.getPortraitOverseasFunds() - LRPmin[7]) / (LRPmax[7] - LRPmin[7])));
            lrp.setPortraitMinisterialCount(LRPmax[8] == LRPmin[8] ? 1.0 : ((lrp.getPortraitMinisterialCount() - LRPmin[8]) / (LRPmax[8] - LRPmin[8])));
            lrp.setPortraitMinisterialFunds(LRPmax[9] == LRPmin[9] ? 1.0 : ((lrp.getPortraitMinisterialFunds() - LRPmin[9]) / (LRPmax[9] - LRPmin[9])));
            lrp.setPortraitProvinceCount(LRPmax[10] == LRPmin[10] ? 1.0 : ((lrp.getPortraitProvinceCount() - LRPmin[10]) / (LRPmax[10] - LRPmin[10])));
            lrp.setPortraitProvinceFunds(LRPmax[11] == LRPmin[11] ? 1.0 : ((lrp.getPortraitProvinceFunds() - LRPmin[11]) / (LRPmax[11] - LRPmin[11])));
            lrp.setPortraitProvinceOtherCount(LRPmax[12] == LRPmin[12] ? 1.0 : ((lrp.getPortraitProvinceOtherCount() - LRPmin[12]) / (LRPmax[12] - LRPmin[12])));
            lrp.setPortraitProvinceOtherFunds(LRPmax[13] == LRPmin[13] ? 1.0 : ((lrp.getPortraitProvinceOtherFunds() - LRPmin[13]) / (LRPmax[13] - LRPmin[13])));
            double score = (lrp.getHorizontalCount() + lrp.getHorizontalFunds() + lrp.getPortraitCountryCount() + lrp.getPortraitCountryFunds() +
                    lrp.getPortraitArmyCount() + lrp.getPortraitArmyFunds() + lrp.getPortraitOverseasCount() + lrp.getPortraitOverseasFunds() +
                    lrp.getPortraitMinisterialCount() + lrp.getPortraitMinisterialFunds() + lrp.getPortraitProvinceCount() + lrp.getPortraitProvinceFunds() +
                    lrp.getPortraitProvinceOtherCount() + lrp.getPortraitProvinceOtherFunds()) / 14;
            scoreLRP.put(lrp.getSchoolName(), score);
        }

        //发表论文情况
        Map<String, Double> scorePL = new HashMap<>();// 存储publishPaper各学校归一化的值 发表论文情况
        double[] PLmax = new double[14];
        double[] PLmin = new double[14];
        for (int i = 0; i < 14; i++) {
            LRPmax[i] = Double.MIN_VALUE;
            LRPmin[i] = Double.MAX_VALUE;
        }
        for (PublishPaper pl : publishPaper) {
            double[] values = {
                    pl.getTotalCount(), pl.getScientificCount(), pl.getTeachCount(), pl.getSCI(), pl.getSSCI(),
                    pl.getEI(), pl.getAHCI(), pl.getCPCI(), pl.getCSCD(), pl.getCSSCI(), pl.getIndustryJointPublication(),
                    pl.getLocalJointPublication(), pl.getInternationalJointPublication(), pl.getInterdisciplinaryPaper()
            };
            for (int i = 0; i < values.length; i++) {
                if (values[i] > PLmax[i]) {
                    PLmax[i] = values[i];
                }
                if (values[i] < PLmin[i]) {
                    PLmin[i] = values[i];
                }
            }
        }
        for (PublishPaper pl : publishPaper) {
            pl.setTotalCount(LRPmax[0] == LRPmin[0] ? 1.0 : ((pl.getTotalCount() - LRPmin[0]) / (LRPmax[0] - LRPmin[0])));
            pl.setScientificCount(LRPmax[1] == LRPmin[1] ? 1.0 : ((pl.getScientificCount() - LRPmin[1]) / (LRPmax[1] - LRPmin[1])));
            pl.setTeachCount(LRPmax[2] == LRPmin[2] ? 1.0 : ((pl.getTeachCount() - LRPmin[2]) / (LRPmax[2] - LRPmin[2])));
            pl.setSCI(LRPmax[3] == LRPmin[3] ? 1.0 : ((pl.getSCI() - LRPmin[3]) / (LRPmax[3] - LRPmin[3])));
            pl.setSSCI(LRPmax[4] == LRPmin[4] ? 1.0 : ((pl.getSSCI() - LRPmin[4]) / (LRPmax[4] - LRPmin[4])));
            pl.setEI(LRPmax[5] == LRPmin[5] ? 1.0 : ((pl.getEI() - LRPmin[5]) / (LRPmax[5] - LRPmin[5])));
            pl.setAHCI(LRPmax[6] == LRPmin[6] ? 1.0 : ((pl.getAHCI() - LRPmin[6]) / (LRPmax[6] - LRPmin[6])));
            pl.setCPCI(LRPmax[7] == LRPmin[7] ? 1.0 : ((pl.getCPCI() - LRPmin[7]) / (LRPmax[7] - LRPmin[7])));
            pl.setCSCD(LRPmax[8] == LRPmin[8] ? 1.0 : ((pl.getCSCD() - LRPmin[8]) / (LRPmax[8] - LRPmin[8])));
            pl.setCSSCI(LRPmax[9] == LRPmin[9] ? 1.0 : ((pl.getCSSCI() - LRPmin[9]) / (LRPmax[9] - LRPmin[9])));
            pl.setIndustryJointPublication(LRPmax[10] == LRPmin[10] ? 1.0 : ((pl.getIndustryJointPublication() - LRPmin[10]) / (LRPmax[10] - LRPmin[10])));
            pl.setLocalJointPublication(LRPmax[11] == LRPmin[11] ? 1.0 : ((pl.getLocalJointPublication() - LRPmin[11]) / (LRPmax[11] - LRPmin[11])));
            pl.setInternationalJointPublication(LRPmax[12] == LRPmin[12] ? 1.0 : ((pl.getInternationalJointPublication() - LRPmin[12]) / (LRPmax[12] - LRPmin[12])));
            pl.setInterdisciplinaryPaper(LRPmax[13] == LRPmin[13] ? 1.0 : ((pl.getInterdisciplinaryPaper() - LRPmin[13]) / (LRPmax[13] - LRPmin[13])));
            double score = (pl.getTotalCount() + pl.getScientificCount() + pl.getTeachCount() + pl.getSCI() + pl.getSSCI() +
                    pl.getEI() + pl.getAHCI() + pl.getCPCI() + pl.getCSCD() + pl.getCSSCI() + pl.getIndustryJointPublication() +
                    pl.getLocalJointPublication() + pl.getInternationalJointPublication() + pl.getInterdisciplinaryPaper()) / 14;
            scorePL.put(pl.getSchoolName(), score);
        }

        //专利专著
        Map<String, Double> scorePM = new HashMap<>();// 存储patentMonographs各学校归一化的值 专利专著
        double[] PMmax = new double[8];
        double[] PMmin = new double[8];
        for (int i = 0; i < 8; i++) {
            LRPmax[i] = Double.MIN_VALUE;
            LRPmin[i] = Double.MAX_VALUE;
        }
        for (PatentMonograph pm : patentMonographs) {
            double[] values = {
                    pm.getMonographCount(), pm.getMonographMonograph(), pm.getBooks(), pm.getInventionPatents(), pm.getUtilityPatents(),
                    pm.getDesignPatents(), pm.getCopyrights(), pm.getIndustryJointPatents()
            };
            for (int i = 0; i < values.length; i++) {
                if (values[i] > PMmax[i]) {
                    PMmax[i] = values[i];
                }
                if (values[i] < PMmin[i]) {
                    PMmin[i] = values[i];
                }
            }
        }
        for (PatentMonograph pm : patentMonographs) {
            pm.setMonographCount(LRPmax[0] == LRPmin[0] ? 1.0 : ((pm.getMonographCount() - LRPmin[0]) / (LRPmax[0] - LRPmin[0])));
            pm.setMonographMonograph(LRPmax[1] == LRPmin[1] ? 1.0 : ((pm.getMonographMonograph() - LRPmin[1]) / (LRPmax[1] - LRPmin[1])));
            pm.setBooks(LRPmax[2] == LRPmin[2] ? 1.0 : ((pm.getBooks() - LRPmin[2]) / (LRPmax[2] - LRPmin[2])));
            pm.setInventionPatents(LRPmax[3] == LRPmin[3] ? 1.0 : ((pm.getInventionPatents() - LRPmin[3]) / (LRPmax[3] - LRPmin[3])));
            pm.setUtilityPatents(LRPmax[4] == LRPmin[4] ? 1.0 : ((pm.getUtilityPatents() - LRPmin[4]) / (LRPmax[4] - LRPmin[4])));
            pm.setDesignPatents(LRPmax[5] == LRPmin[5] ? 1.0 : ((pm.getDesignPatents() - LRPmin[5]) / (LRPmax[5] - LRPmin[5])));
            pm.setCopyrights(LRPmax[6] == LRPmin[6] ? 1.0 : ((pm.getCopyrights() - LRPmin[6]) / (LRPmax[6] - LRPmin[6])));
            pm.setIndustryJointPatents(LRPmax[7] == LRPmin[7] ? 1.0 : ((pm.getIndustryJointPatents() - LRPmin[7]) / (LRPmax[7] - LRPmin[7])));
            double score = (pm.getMonographCount() + pm.getMonographMonograph() + pm.getBooks() + pm.getInventionPatents() + pm.getUtilityPatents() +
                    pm.getDesignPatents() + pm.getCopyrights() + pm.getIndustryJointPatents()) / 8;
            scorePM.put(pm.getSchoolName(), score);
        }

        //教师科研成果转化情况
        Map<String, Double> scoreRTS = new HashMap<>();// 存储resTransferStatus各学校归一化的值 教师科研成果转化情况
        double RTSScore = 0.0;
        String RTSschoolNameOld = "";
        for (int i = 0; i < resTransferStatus.size(); i++) {
            if (i == 0 || resTransferStatus.get(i).getSchoolName().equals(RTSschoolNameOld)) {
                RTSScore += resTransferStatus.get(i).getScore();
            } else if (!resTransferStatus.get(i).getSchoolName().equals(RTSschoolNameOld)) {
                scoreRTS.put(RTSschoolNameOld, RTSScore);
                RTSScore = 0.0;
            }
            RTSschoolNameOld = resTransferStatus.get(i).getSchoolName();
            if(i == resTransferStatus.size() - 1){
                scoreRTS.put(RTSschoolNameOld, RTSScore);
            }
            if(resTransferStatus.size() == 1){
                scoreRTS.put(RTSschoolNameOld, RTSScore);
            }
        }

        //教师科研成果转化情况 的归一化处理
        // 初始化最大值和最小值
        double max = Double.MIN_VALUE;
        double min = Double.MAX_VALUE;
        // 找出 map 中的最大值和最小值
        for (double value : scoreRTS.values()) {
            if (value > max) {
                max = value;
            }
            if (value < min) {
                min = value;
            }
        }
        // 处理最大值和最小值相等的情况，避免除零错误
        if (max == min) {
            // 当最大值和最小值相等时，归一化后的值都设为 0
            scoreRTS.replaceAll((k, v) -> 1.0);
        }else {
            // 进行归一化处理
            for (Map.Entry<String, Double> entry : scoreRTS.entrySet()) {
                String key = entry.getKey();
                double value = entry.getValue();
                // 使用最小 - 最大归一化公式进行计算
                double normalizedValue = (value - min) / (max - min);
                scoreRTS.put(key, normalizedValue);
            }
        }

        // 对四个数值进行合并计算求平均值
        Map<String, Double> researchLevel = new HashMap<>();
        // 收集所有学校的名称（三个map的key的并集）
        Set<String> allSchools = new HashSet<>();
        allSchools.addAll(scoreLRP.keySet());
        allSchools.addAll(scorePL.keySet());
        allSchools.addAll(scorePM.keySet());
        allSchools.addAll(scoreRTS.keySet());
        // 遍历所有学校，计算每个学校的平均分
        for (String school : allSchools) {
            double sum = 0.0;
            if (scoreLRP.containsKey(school)) {
                sum += scoreLRP.get(school);
            }
            if (scorePL.containsKey(school)) {
                sum += scorePL.get(school);
            }
            if (scorePM.containsKey(school)) {
                sum += scorePM.get(school);
            }
            if (scoreRTS.containsKey(school)) {
                sum += scoreRTS.get(school);
            }
            double average = Math.round(sum / 4 * 10000.0) / 10000.0;
            researchLevel.put(school, average);
        }

        result = jsonPut(result, schoolCode, researchLevel, "kysp");
        return result;
    }

    //专业实验教学校内场所情况
    public JSONObject[] getLaboratoryCampus(JSONObject[] result, String year, List<String> schoolCode, String majorName) {
        List<LaboratoryCampus> laboratoryCampuses = majorSourceService.getLaboratoryCampus(year, schoolCode, majorName);
        if (laboratoryCampuses.isEmpty()) {
            for (int i = 0; i < schoolCode.size(); i++) {
                result[i].put("syjx", "-");
            }
            return result;
        }
        Map<String, Double> scoreLC = new HashMap<>();// 存储leadResearchProjects各学校归一化的值 主持科研项目
        double[] LPmax = new double[6];
        double[] LPmin = new double[6];
        for (int i = 0; i < 6; i++) {
            LPmax[i] = -Double.MIN_VALUE;
            LPmin[i] = Double.MAX_VALUE;
        }
        for (LaboratoryCampus ly : laboratoryCampuses) {
            double[] values = {
                   ly.getBasicLabCount(),ly.getBasicLabArea(),ly.getBasicLabEquipmentValue(),ly.getMajorLabCount(),ly.getMajorLabArea(),ly.getMajorLabEquipmentValue()
            };
            for (int i = 0; i < values.length; i++) {
                if (values[i] > LPmax[i]) {
                    LPmax[i] = values[i];
                }
                if (values[i] < LPmin[i]) {
                    LPmin[i] = values[i];
                }
            }
        }
        for (LaboratoryCampus ly : laboratoryCampuses) {
            ly.setBasicLabCount(LPmax[0] == LPmin[0] ? 1.0 : (ly.getBasicLabCount() - LPmin[0]) / (LPmax[0] - LPmin[0]));
            ly.setBasicLabArea(LPmax[1] == LPmin[1] ? 1.0 : (ly.getBasicLabArea() - LPmin[1]) / (LPmax[1] - LPmin[1]));
            ly.setBasicLabEquipmentValue(LPmax[2] == LPmin[2] ? 1.0 : (ly.getBasicLabEquipmentValue() - LPmin[2]) / (LPmax[2] - LPmin[2]));
            ly.setMajorLabCount(LPmax[3] == LPmin[3] ? 1.0 : (ly.getMajorLabCount() - LPmin[3]) / (LPmax[3] - LPmin[3]));
            ly.setMajorLabArea(LPmax[4] == LPmin[4] ? 1.0 : (ly.getMajorLabArea() - LPmin[4]) / (LPmax[4] - LPmin[4]));
            ly.setMajorLabEquipmentValue(LPmax[5] == LPmin[5] ? 1.0 : (ly.getMajorLabEquipmentValue() - LPmin[5]) / (LPmax[5] - LPmin[5]));
            double score = (ly.getBasicLabCount() + ly.getBasicLabArea() + ly.getBasicLabEquipmentValue() + ly.getMajorLabCount() + ly.getMajorLabArea() + ly.getMajorLabEquipmentValue()) / 6;
            score = Math.round( score* 10000.0) / 10000.0;
            scoreLC.put(ly.getSchoolName(), score);
        }

        result = jsonPut(result , schoolCode,scoreLC,"syjx");
        return result;
    }

    //实习实践实训基地情况
    public JSONObject[] trainingBase(JSONObject[] result, String year, List<String> schoolCode, String majorName) {
        //实习实践实训基地情况-省级及以上本科教学项目建设情况中相关基地情况
        List<ProvincialProjectBase> provincialProjectBase = majorSourceService.getProvincialProjectBase(year, schoolCode, majorName);
        //实习实践实训基地情况-实习实践实训基地情况
        List<PracticalBaseStatus> practicalBaseStatuses = majorSourceService.getPracticalBaseStatus(year, schoolCode, majorName);
        if (provincialProjectBase.isEmpty() && practicalBaseStatuses.isEmpty()) {
            for (int i = 0; i < schoolCode.size(); i++) {
                result[i].put("sxsjsxjdqk", "-");
            }
            return result;
        }
        //实习实践实训基地情况-省级及以上本科教学项目建设情况中相关基地情况
        Map<String, Double> scorePPB = new HashMap<>();
        double PPBcore = 0.0;
        String PPBschoolNameOld = "";
        for (int i = 0; i < provincialProjectBase.size(); i++) {
            if (i == 0 || provincialProjectBase.get(i).getSchoolName().equals(PPBschoolNameOld)) {
                PPBcore += provincialProjectBase.get(i).getLevelScore();
            } else if (!provincialProjectBase.get(i).getSchoolName().equals(PPBschoolNameOld)) {
                scorePPB.put(PPBschoolNameOld, PPBcore);
                PPBcore = 0.0;
            }
            PPBschoolNameOld = provincialProjectBase.get(i).getSchoolName();
            if (i == provincialProjectBase.size() - 1) {
                scorePPB.put(PPBschoolNameOld, PPBcore);
            }
            if(provincialProjectBase.size() == 1){
                scorePPB.put(PPBschoolNameOld, PPBcore);
            }
        }

        //归一化处理
        // 初始化最大值和最小值
        Double max = null;
        Double min = null;
        // 找出 map 中的最大值和最小值
        for (double value : scorePPB.values()) {
            if (max == null || value > max) {
                max = value;
            }
            if (min == null || value < min) {
                min = value;
            }
        }
        // 处理最大值和最小值相等的情况，避免除零错误
        if (max == min) {
            // 当最大值和最小值相等时，归一化后的值都设为 0
            scorePPB.replaceAll((k, v) -> 1.0);
        } else if(max.equals(0.0)){
            scorePPB.replaceAll((k, v) -> 0.0);
        } else {
            // 进行归一化处理
            for (Map.Entry<String, Double> entry : scorePPB.entrySet()) {
                String key = entry.getKey();
                double value = entry.getValue();
                // 使用最小 - 最大归一化公式进行计算
                double normalizedValue = (value - min) / (max - min);
                scorePPB.put(key, normalizedValue);
            }
        }

        //实习实践实训基地情况-实习实践实训基地情况
        Map<String, Double> scorePBS = new HashMap<>();
        double PBScore = 0.0;
        String PBSschoolNameOld = "";
        for (int i = 0; i < practicalBaseStatuses.size(); i++) {
            if (i == 0 || practicalBaseStatuses.get(i).getSchoolName().equals(PBSschoolNameOld)) {
                PBScore += practicalBaseStatuses.get(i).getStudentsCountCurrent();
            } else if (!practicalBaseStatuses.get(i).getSchoolName().equals(PBSschoolNameOld) || i == practicalBaseStatuses.size() - 1) {
                scorePBS.put(PBSschoolNameOld, PBScore);
                PBScore = 0.0;
            }
            PBSschoolNameOld = practicalBaseStatuses.get(i).getSchoolName();
            if(practicalBaseStatuses.size() == 1){
                scorePBS.put(PBSschoolNameOld, PBScore);
            }
        }

        //教师科研成果转化情况 的归一化处理
        // 初始化最大值和最小值
        Double max2 = null;
        Double min2 = null;
        // 找出 map 中的最大值和最小值
        for (double value : scorePBS.values()) {
            if (max2 == null || value > max2) {
                max2 = value;
            }
            if (min2 == null || value < min2) {
                min2 = value;
            }
        }
        // 处理最大值和最小值相等的情况，避免除零错误
        if (max2 == min2) {
            scorePBS.replaceAll((k, v) -> 1.0);
        }else if(max2.equals(0.0)) {
            scorePBS.replaceAll((k, v) -> 0.0);
        } else {
            // 进行归一化处理
            for (Map.Entry<String, Double> entry : scorePBS.entrySet()) {
                String key = entry.getKey();
                double value = entry.getValue();
                // 使用最小 - 最大归一化公式进行计算
                double normalizedValue = (value - min2) / (max2 - min2);
                scorePBS.put(key, normalizedValue);
            }
        }

        // 对两个数值进行合并计算求平均值
        Map<String, Double> trainingBase = new HashMap<>();
        // 收集所有学校的名称（三个map的key的并集）
        Set<String> allSchools = new HashSet<>();
        allSchools.addAll(scorePPB.keySet());
        allSchools.addAll(scorePBS.keySet());
        // 遍历所有学校，计算每个学校的平均分
        for (String school : allSchools) {
            double sum = 0.0;
            if (scorePPB.containsKey(school)) {
                sum += scorePPB.get(school);
            }
            if (scorePBS.containsKey(school)) {
                sum += scorePBS.get(school);
            }
            double average = Math.round(sum / 2 * 10000.0) / 10000.0;
            trainingBase.put(school, average);
        }
        result = jsonPut(result, schoolCode, trainingBase, "sxsjsxjdqk");
        return result;
    }

    // 一流课程建设
    public JSONObject[] getTopCourse(JSONObject[] result, String year, List<String> schoolCode, String majorName) {
        List<TopCourse> topCourse = majorSourceService.getTopCourse(year, schoolCode, majorName);
        if (topCourse.isEmpty()) {
            for (int i = 0; i < schoolCode.size(); i++) {
                result[i].put("ylkcjs", "-");
            }
            return result;
        }
        //一流课程建设
        Map<String, Double> scoreTC = new HashMap<>();
        double TCScore = 0.0;
        String TCschoolNameOld = "";
        for (int i = 0; i < topCourse.size(); i++) {
            if (i == 0 || topCourse.get(i).getSchoolName().equals(TCschoolNameOld)) {
                TCScore += topCourse.get(i).getLevelScore();
            } else if (!topCourse.get(i).getSchoolName().equals(TCschoolNameOld)) {
                scoreTC.put(TCschoolNameOld, TCScore);
                TCScore = topCourse.get(i).getLevelScore();
            }
            TCschoolNameOld = topCourse.get(i).getSchoolName();
            if(i == topCourse.size() - 1){
                scoreTC.put(TCschoolNameOld, TCScore);
            }
            if(topCourse.size() == 1){
                scoreTC.put(TCschoolNameOld, TCScore);
            }
        }

        //归一化处理
        // 初始化最大值和最小值
        double max = Double.MIN_VALUE;
        double min = Double.MAX_VALUE;
        // 找出 map 中的最大值和最小值
        for (double value : scoreTC.values()) {
            if (value > max) {
                max = value;
            }
            if (value < min) {
                min = value;
            }
        }
        // 处理最大值和最小值相等的情况，避免除零错误
        if (max == min) {
            scoreTC.replaceAll((k, v) -> 1.0); // 当最大值和最小值相等时，归一化后的值都设为 1.0
        } else if(max == 0){
            scoreTC.replaceAll((k, v) -> 0.0);
        } else {
            // 进行归一化处理
            for (Map.Entry<String, Double> entry : scoreTC.entrySet()) {
                String key = entry.getKey();
                double value = entry.getValue();
                // 使用最小 - 最大归一化公式进行计算
                double normalizedValue = (value - min) / (max - min);
                scoreTC.put(key, normalizedValue);
            }
        }

        result = jsonPut(result, schoolCode, scoreTC, "ylkcjs");
        return result;
    }

    // 出版教材质量
    public JSONObject[] publishTextbooks(JSONObject[] result, String year, List<String> schoolCode, String majorName) {
        List<Textbook> textbooks =  majorSourceService.getTextbook(year, schoolCode, majorName);
        if (textbooks.isEmpty()){
            for (int i = 0; i < schoolCode.size(); i++) {
                result[i].put("cbjczl", "-");
            }
        }
        Map<String, Double> scorePT = new HashMap<>();
        Double max = null;
        Double min = null;
        // 找出 map 中的最大值和最小值
        for (Textbook textbook : textbooks) {
            double score = textbook.getTotalScore();
            if (max == null || score > max) {
                max = score;
            }
            if (min == null || score < min) {
                min = score;
            }
        }
        if (Objects.equals(max, min) && !max.equals(0.0)) {
            for (Textbook textbook : textbooks) {
                scorePT.put(textbook.getSchoolName(), 1.0);
            }
        } else if(max.equals(0.0)){
            for (Textbook textbook : textbooks) {
                scorePT.put(textbook.getSchoolName(), 0.0);
            }
        } else {
            for (Textbook textbook : textbooks) {
                double score = (textbook.getTotalScore() - min) / (max - min);
                scorePT.put(textbook.getSchoolName(), score);
            }
        }
        result = jsonPut(result, schoolCode, scorePT, "cbjczl");

        return result;
    }

    //专业课程教学满意度
    public JSONObject[] teachingSatisfaction(JSONObject[] result, String year, List<String> schoolCode, String majorName) {
        for(int i = 0; i < schoolCode.size(); i++){
            result[i].put("zykcjxmyd", "问卷数据");
        }
        return result;
    }

    //信息化课程建设
    public JSONObject[] informationCourse(JSONObject[] result, String year, List<String> schoolCode, String majorName,String subject) {
        String tabelName = "";
        if (subject.equals("工学")) {
            tabelName = "base_gkgklzykcqk";
        }
        List<InfoCourseBuild> infoCourseBuilds = majorSourceService.getInfoCourseBuild(tabelName, year, schoolCode, majorName);
        if (infoCourseBuilds.isEmpty()) {
            for (int i = 0; i < schoolCode.size(); i++) {
                result[i].put("xxhkcjs", "-");
            }
            return result;
        }

        Map<String, Double> scoreICB = new HashMap<>();
        double ICBcore = 0.0;
        String ICBschoolNameOld = "";
        for (int i = 0; i < infoCourseBuilds.size(); i++) {
            if (i == 0 || infoCourseBuilds.get(i).getSchoolName().equals(ICBschoolNameOld)) {
                ICBcore += infoCourseBuilds.get(i).getScore();
            } else if (!infoCourseBuilds.get(i).getSchoolName().equals(ICBschoolNameOld)) {
                scoreICB.put(ICBschoolNameOld, ICBcore);
                ICBcore = 0.0;
            }
            ICBschoolNameOld = infoCourseBuilds.get(i).getSchoolName();
            if(i == infoCourseBuilds.size() - 1){
                scoreICB.put(ICBschoolNameOld, ICBcore);
            }
            if(infoCourseBuilds.size() == 1){
                scoreICB.put(ICBschoolNameOld, ICBcore);
            }
        }

        //归一化处理
        // 初始化最大值和最小值
        double max = -Double.MIN_VALUE;
        double min = Double.MAX_VALUE;
        // 找出 map 中的最大值和最小值
        for (double value : scoreICB.values()) {
            if (value > max) {
                max = value;
            }
            if (value < min) {
                min = value;
            }
        }
        // 处理最大值和最小值相等的情况，避免除零错误
        if (max == min) {
            scoreICB.replaceAll((k, v) -> 1.0); // 当最大值和最小值相等时，归一化后的值都设为 1.0
        } else if(max == 0){
            scoreICB.replaceAll((k, v) -> 0.0);
        } else {
            // 进行归一化处理
            for (Map.Entry<String, Double> entry : scoreICB.entrySet()) {
                String key = entry.getKey();
                double value = entry.getValue();
                // 使用最小 - 最大归一化公式进行计算
                double normalizedValue = (value - min) / (max - min);
                scoreICB.put(key, normalizedValue);
            }
        }

        result = jsonPut(result, schoolCode, scoreICB, "xxhkcjs");
        return result;
    }

    //专业素养
    public JSONObject[] professionalCompetence(JSONObject[] result, String year, List<String> schoolCode, String majorName) {
        for(int i = 0; i < schoolCode.size(); i++){
            result[i].put("zysy", "问卷数据");
        }
        return result;
    }

    //通用能力
    public JSONObject[] generalAbility(JSONObject[] result, String year, List<String> schoolCode, String majorName) {
        for(int i = 0; i < schoolCode.size(); i++){
            result[i].put("tynl", "问卷数据");
        }
        return result;
    }

    //高影响力教育活动参与情况
    public JSONObject[] participation(JSONObject[] result, String year, List<String> schoolCode, String majorName) {
        //高影响力教育活动参与情况-参加创新创业训练计划
        List<InnovationParticipation> innovationParticipations = majorSourceService.getInnovationParticipation(year, schoolCode, majorName);
        //高影响力教育活动参与情况-参加教师科研情况
        List<TeacherResPartic> teacherResPartics = majorSourceService.getTeacherResPartic(year, schoolCode, majorName);
        //高影响力教育活动参与情况-本科生交流情况
        List<UndergradExchange> undergradExchanges = majorSourceService.getUndergradExchange(year, schoolCode, majorName);
        if (innovationParticipations.isEmpty() && teacherResPartics.isEmpty() && undergradExchanges.isEmpty()) {
            for (int i = 0; i < schoolCode.size(); i++) {
                result[i].put("gyxljyhdcyqk", "-");
            }
            return result;
        }
        //参加创新创业训练计划
        Map<String, Double> scoreIP = new HashMap<>();// 存储leadResearchProjects各学校归一化的值 主持科研项目
        double[] IPmax = new double[2];
        double[] IPmin = new double[2];
        for (int i = 0; i < 2; i++) {
            IPmax[i] = -Double.MIN_VALUE;
            IPmin[i] = Double.MAX_VALUE;
        }
        for (InnovationParticipation ip : innovationParticipations) {
            double[] values = {
                    ip.getCountryCount(),ip.getProvinceCount()
            };
            for (int i = 0; i < values.length; i++) {
                if (values[i] > IPmax[i]) {
                    IPmax[i] = values[i];
                }
                if (values[i] < IPmin[i]) {
                    IPmin[i] = values[i];
                }
            }
        }
        for (InnovationParticipation ip : innovationParticipations) {
            ip.setCountryCount(IPmax[0] == IPmin[0] ? 1.0 : (ip.getCountryCount() - IPmin[0]) / (IPmax[0] - IPmin[0]));
            ip.setProvinceCount(IPmax[1] == IPmin[1] ? 1.0 : (ip.getProvinceCount() - IPmin[1]) / (IPmax[1] - IPmin[1]));
            double score = (ip.getCountryCount() + ip.getProvinceCount())/2;
            score = Math.round( score* 10000.0) / 10000.0;
            scoreIP.put(ip.getSchoolName(), score);
        }


        //参加教师科研情况
        Map<String, Double> scoreTRP = new HashMap<>();// 存储leadResearchProjects各学校归一化的值 主持科研项目
        double[] TRPmax = new double[2];
        double[] TRPmin = new double[2];
        for (int i = 0; i < 2; i++) {
            TRPmax[i] = -Double.MIN_VALUE;
            TRPmin[i] = Double.MAX_VALUE;
        }
        for (TeacherResPartic trp : teacherResPartics) {
            double[] values = {
                    trp.getProjectCount(), trp.getStudentCount()
            };
            for (int i = 0; i < values.length; i++) {
                if (values[i] > TRPmax[i]) {
                    TRPmax[i] = values[i];
                }
                if (values[i] < TRPmin[i]) {
                    TRPmin[i] = values[i];
                }
            }
        }
        for (TeacherResPartic trp : teacherResPartics) {
            trp.setProjectCount(TRPmax[0] == TRPmin[0] ? 1.0 : (trp.getProjectCount() - TRPmin[0]) / (TRPmax[0] - TRPmin[0]));
            trp.setStudentCount(TRPmax[1] == TRPmin[1] ? 1.0 : (trp.getStudentCount() - TRPmin[1]) / (TRPmax[1] - TRPmin[1]));
            double score = (trp.getProjectCount() + trp.getStudentCount())/2;
            score = Math.round( score* 10000.0) / 10000.0;
            scoreTRP.put(trp.getSchoolName(), score);
        }

        //本科生交流情况
        Map<String, Double> scoreUE = new HashMap<>();// 存储leadResearchProjects各学校归一化的值 主持科研项目
        double[] UEmax = new double[2];
        double[] UEmin = new double[2];
        for (int i = 0; i < 2; i++) {
            UEmax[i] = -Double.MIN_VALUE;
            UEmin[i] = Double.MAX_VALUE;
        }
        for (UndergradExchange ue : undergradExchanges) {
            double[] values = {
                    ue.getOutCount(), ue.getInCount()
            };
            for (int i = 0; i < values.length; i++) {
                if (values[i] > UEmax[i]) {
                    UEmax[i] = values[i];
                }
                if (values[i] < UEmin[i]) {
                    UEmin[i] = values[i];
                }
            }
        }
        for (UndergradExchange ue : undergradExchanges) {
            ue.setOutCount(TRPmax[0] == TRPmin[0] ? 1.0 : (ue.getOutCount() - TRPmin[0]) / (TRPmax[0] - TRPmin[0]));
            ue.setInCount(TRPmax[1] == TRPmin[1] ? 1.0 : (ue.getInCount() - TRPmin[1]) / (TRPmax[1] - TRPmin[1]));
            double score = (ue.getOutCount() + ue.getInCount())/2;
            score = Math.round( score* 10000.0) / 10000.0;
            scoreUE.put(ue.getSchoolName(), score);
        }


        // 对三个数值进行合并计算求平均值
        Map<String, Double> participation = new HashMap<>();
        // 收集所有学校的名称（三个map的key的并集）
        Set<String> allSchools = new HashSet<>();
        allSchools.addAll(scoreIP.keySet());
        allSchools.addAll(scoreTRP.keySet());
        allSchools.addAll(scoreUE.keySet());
        // 遍历所有学校，计算每个学校的平均分
        for (String school : allSchools) {
            double sum = 0.0;
            if (scoreIP.containsKey(school)) {
                sum += scoreIP.get(school);
            }
            if (scoreTRP.containsKey(school)) {
                sum += scoreTRP.get(school);
            }
            if (scoreUE.containsKey(school)) {
                sum += scoreUE.get(school);
            }
            double average = Math.round(sum / 3 * 10000.0) / 10000.0;
            participation.put(school, average);
        }

        result = jsonPut(result, schoolCode, participation, "gyxljyhdcyqk");
        return result;
    }

    // 学习成效
    public JSONObject[] learnEffective(JSONObject[] result, String year, List<String> schoolCode, String majorName) {
        List<LearnResult> learnResults = majorSourceService.getLearnResult(year, schoolCode, majorName);
        if(learnResults.size() == 0){
            for(int i = 0; i < schoolCode.size(); i++){
                result[i].put("xxcx", "-");
            }
            return result;
        }

        Map<String, Double> scoreLE = new HashMap<>();
        double[] LEmax = new double[10];
        double[] LEmin = new double[10];
        for (int i = 0; i < 10; i++) {
            LEmax[i] = -Double.MIN_VALUE;
            LEmin[i] = Double.MAX_VALUE;
        }
        for (LearnResult lr : learnResults) {
            double[] values = {
                    lr.getPubPapers(), lr.getReprWorks(), lr.getCopyright(), lr.getNationalAwards(),
                    lr.getProvinceAwards(), lr.getCompAwardsTotal(), lr.getInternationalAwards(),
                    lr.getArtNationalAwards(), lr.getArtAreaAwards(), lr.getArtCompAwards()
            };
            for (int i = 0; i < values.length; i++) {
                if (values[i] > LEmax[i]) {
                    LEmax[i] = values[i];
                }
                if (values[i] < LEmin[i]) {
                    LEmin[i] = values[i];
                }
            }
        }
        for (LearnResult lr : learnResults) {
            lr.setPubPapers(LEmax[0] == LEmin[0] ? 1.0 : (lr.getPubPapers() - LEmin[0]) / (LEmax[0] - LEmin[0]));
            lr.setReprWorks(LEmax[1] == LEmin[1] ? 1.0 : (lr.getReprWorks() - LEmin[1]) / (LEmax[1] - LEmin[1]));
            lr.setCopyright(LEmax[2] == LEmin[2] ? 1.0 : (lr.getCopyright() - LEmin[2]) / (LEmax[2] - LEmin[2]));
            lr.setNationalAwards(LEmax[3] == LEmin[3] ? 1.0 : (lr.getNationalAwards() - LEmin[3]) / (LEmax[3] - LEmin[3]));
            lr.setProvinceAwards(LEmax[4] == LEmin[4] ? 1.0 : (lr.getProvinceAwards() - LEmin[4]) / (LEmax[4] - LEmin[4]));
            lr.setCompAwardsTotal(LEmax[5] == LEmin[5] ? 1.0 : (lr.getCompAwardsTotal() - LEmin[5]) / (LEmax[5] - LEmin[5]));
            lr.setInternationalAwards(LEmax[6] == LEmin[6] ? 1.0 : (lr.getInternationalAwards() - LEmin[6]) / (LEmax[6] - LEmin[6]));
            lr.setArtNationalAwards(LEmax[7] == LEmin[7] ? 1.0 : (lr.getArtNationalAwards() - LEmin[7]) / (LEmax[7] - LEmin[7]));
            lr.setArtAreaAwards(LEmax[8] == LEmin[8] ? 1.0 : (lr.getArtAreaAwards() - LEmin[8]) / (LEmax[8] - LEmin[8]));
            lr.setArtCompAwards(LEmax[9] == LEmin[9] ? 1.0 : (lr.getArtCompAwards() - LEmin[9]) / (LEmax[9] - LEmin[9]));
            double score = (lr.getPubPapers() + lr.getReprWorks() + lr.getCopyright() + lr.getNationalAwards() +
                    lr.getProvinceAwards() + lr.getCompAwardsTotal() + lr.getInternationalAwards() +
                    lr.getArtNationalAwards() + lr.getArtAreaAwards() + lr.getArtCompAwards())/10;
            score = Math.round( score* 10000.0) / 10000.0;
            scoreLE.put(lr.getSchoolName(), score);
        }
        result = jsonPut(result, schoolCode, scoreLE, "xxcx");
        return result;
    }

    // 毕业综合训练情况
    public JSONObject[] graduateTrain(JSONObject[] result, String year, List<String> schoolCode, String majorName) {
        //毕业综合训练情况-毕业综合训练情况
        List<GradCompTraining> gradCompTrainings = majorSourceService.getGradCompTraining(year, schoolCode, majorName);
        //毕业综合训练情况-教师指导学生毕业综合训练情况-教师数量
        List<EmploymentNumber> graduateTrainTeachers = majorSourceService.graduateTrainTeacher(year, schoolCode, majorName);
        //毕业综合训练情况-教师指导学生毕业综合训练情况-学生数量
        List<EmploymentNumber> graduateTrainStu = majorSourceService.graduateTrainStu(year, schoolCode, majorName);

        if (gradCompTrainings.isEmpty() && graduateTrainTeachers.isEmpty() && graduateTrainStu.isEmpty()) {
            for (int i = 0; i < schoolCode.size(); i++) {
                result[i].put("byzhxlqk", "-");
            }
            return result;
        }

        Map<String, Double> scoreGCT = new HashMap<>();
        double GCTmax = -Double.MIN_VALUE;
        double GCTmin = Double.MAX_VALUE;
        for (GradCompTraining gct : gradCompTrainings) {
            double values = gct.getYesRate();
            if (values > GCTmax) {
                GCTmax = values;
            }
            if (values < GCTmin) {
                GCTmin = values;
            }
        }
        for (GradCompTraining gct : gradCompTrainings) {
            gct.setYesRate(GCTmax == GCTmin ? 1.0 : (gct.getYesRate() - GCTmin) / (GCTmax - GCTmin));
            double score = Math.round(gct.getYesRate() * 10000.0) / 10000.0;
            scoreGCT.put(gct.getSchoolName(), score);
        }


        // 创建一个空的 Map 用于存储结果
        Map<String, Double> graduateTrainTeaStu = new HashMap<>();

        // 将 ListA 中的数据存入一个 Map 中，键为学校名称，值为 count
        Map<String, Double> mapA = new HashMap<>();
        for (EmploymentNumber en : graduateTrainTeachers) {
            mapA.put(en.getSchoolName(), en.getCount());
        }

        // 将 ListB 中的数据存入一个 Map 中，键为学校名称，值为 count
        Map<String, Double> mapB = new HashMap<>();
        for (EmploymentNumber en : graduateTrainStu) {
            mapB.put(en.getSchoolName(), en.getCount());
        }

        // 合并两个 Map 的键集，得到所有学校名称的集合
        Set<String> allSchool = new HashSet<>();
        allSchool.addAll(mapA.keySet());
        allSchool.addAll(mapB.keySet());
        // 遍历所有学校名称
        for (String schoolName : allSchool) {
            double countTea = mapA.getOrDefault(schoolName, 0.0);
            double countStu = mapB.getOrDefault(schoolName, 0.0);
            // 如果 countB 为 0，避免除零错误，将结果设为 0
            double ratio = countStu == 0 ? 0 : countStu / countTea;
            graduateTrainTeaStu.put(schoolName, ratio);
        }


        // 对三个数值进行合并计算求平均值
        Map<String, Double> graduateTrain = new HashMap<>();
        // 收集所有学校的名称（三个map的key的并集）
        Set<String> allSchools = new HashSet<>();
        allSchools.addAll(scoreGCT.keySet());
        allSchools.addAll(graduateTrainTeaStu.keySet());
        // 遍历所有学校，计算每个学校的平均分
        for (String school : allSchools) {
            double sum = 0.0;
            if (scoreGCT.containsKey(school)) {
                sum += scoreGCT.get(school);
            }
            if (graduateTrainTeaStu.containsKey(school)) {
                sum += graduateTrainTeaStu.get(school);
            }
            double average = Math.round(sum / 2 * 10000.0) / 10000.0;
            graduateTrain.put(school, average);
        }

        result = jsonPut(result, schoolCode, graduateTrain, "byzhxlqk");
        return result;
    }

    //就业率
    public JSONObject[] employRate(JSONObject[] result, String year, List<String> schoolCode, String majorName) {
        // 就业学生数量
        List<EmploymentNumber> employmentNumbers = majorSourceService.employmentNumbers(year, schoolCode, majorName);
        // 毕业学生数量
        List<EmploymentNumber> getGraduateNumbers = majorSourceService.getGraduateNumbers(year, schoolCode, majorName);
        if (employmentNumbers.isEmpty() || getGraduateNumbers.isEmpty()) {
            for (int i = 0; i < schoolCode.size(); i++) {
                result[i].put("jyl", "-");
            }
            return result;
        }


        // 创建一个空的 Map 用于存储结果
        Map<String, Double> graduateTrainTeaStu = new HashMap<>();

        // 将 ListA 中的数据存入一个 Map 中，键为学校名称，值为 count
        Map<String, Double> mapA = new HashMap<>();
        for (EmploymentNumber en : employmentNumbers) {
            mapA.put(en.getSchoolName(), en.getCount());
        }
        // 将 ListB 中的数据存入一个 Map 中，键为学校名称，值为 count
        Map<String, Double> mapB = new HashMap<>();
        for (EmploymentNumber gn : getGraduateNumbers) {
            mapB.put(gn.getSchoolName(), gn.getCount());
        }

        // 合并两个 Map 的键集，得到所有学校名称的集合
        Set<String> allSchool = new HashSet<>();
        allSchool.addAll(mapA.keySet());
        allSchool.addAll(mapB.keySet());
        // 遍历所有学校名称
        for (String schoolName : allSchool) {
            double countTea = mapA.getOrDefault(schoolName, 0.0);
            double countStu = mapB.getOrDefault(schoolName, 0.0);
            // 避免除零错误，将结果设为 0
            double ratio = countStu == 0 ? 0 : countTea / countStu;
            graduateTrainTeaStu.put(schoolName, ratio);
        }

        // 归一化处理
        double minScore = Collections.min(graduateTrainTeaStu.values());
        double maxScore = Collections.max(graduateTrainTeaStu.values());
        if(maxScore == minScore){
            for (int i = 0; i < schoolCode.size(); i++) {
                result[i].put("jyl", "1");
            }
            return result;
        }else {
            for (Map.Entry<String, Double> entry : graduateTrainTeaStu.entrySet()) {

                double score = Math.round((entry.getValue() - minScore) / (maxScore - minScore) * 10000.0) / 10000.0;
                graduateTrainTeaStu.put(entry.getKey(), score);
            }
        }
        result = jsonPut(result, schoolCode, graduateTrainTeaStu, "jyl");
        return result;
    }

    // 升学率
    public JSONObject[] higherSchoolRate(JSONObject[] result, String year, List<String> schoolCode, String majorName) {
        // 升学学生数量
        List<EmploymentNumber> higherSchoolNumbers =  majorSourceService.getHigherSchoolNumber(year,schoolCode,majorName);
        // 毕业学生数量
        List<EmploymentNumber> graduateNumbers = majorSourceService.getGraduateNumbers(year, schoolCode, majorName);

        if (higherSchoolNumbers.isEmpty() || graduateNumbers.isEmpty()) {
            for (int i = 0; i < schoolCode.size(); i++) {
                result[i].put("jyl", "-");
            }
            return result;
        }
        // 创建一个空的 Map 用于存储结果
        Map<String, Double> higherSchoolRate = new HashMap<>();

        // 将 ListA 中的数据存入一个 Map 中，键为学校名称，值为 count
        Map<String, Double> mapA = new HashMap<>();
        for (EmploymentNumber hsn : higherSchoolNumbers) {
            mapA.put(hsn.getSchoolName(), hsn.getCount());
        }
        // 将 ListB 中的数据存入一个 Map 中，键为学校名称，值为 count
        Map<String, Double> mapB = new HashMap<>();
        for (EmploymentNumber gn : graduateNumbers) {
            mapB.put(gn.getSchoolName(), gn.getCount());
        }

        // 合并两个 Map 的键集，得到所有学校名称的集合
        Set<String> allSchool = new HashSet<>();
        allSchool.addAll(mapA.keySet());
        allSchool.addAll(mapB.keySet());
        // 遍历所有学校名称
        for (String schoolName : allSchool) {
            double countTea = mapA.getOrDefault(schoolName, 0.0);
            double countStu = mapB.getOrDefault(schoolName, 0.0);
            // 避免除零错误，将结果设为 0
            double ratio = countStu == 0 ? 0 : countTea / countStu;
            higherSchoolRate.put(schoolName, ratio);
        }

        // 归一化处理
        double minScore = Collections.min(higherSchoolRate.values());
        double maxScore = Collections.max(higherSchoolRate.values());
        if(maxScore == minScore){
            for (int i = 0; i < schoolCode.size(); i++) {
                result[i].put("jyl", "1");
            }
            return result;
        }else {
            for (Map.Entry<String, Double> entry : higherSchoolRate.entrySet()) {
                double score = Math.round((entry.getValue() - minScore) / (maxScore - minScore) * 10000.0) / 10000.0;
                higherSchoolRate.put(entry.getKey(), score);
            }
        }
        result = jsonPut(result, schoolCode, higherSchoolRate, "kyl");
        return result;
    }

    //就业去向
    public JSONObject[] EmployDestination(JSONObject[] result, String year, List<String> schoolCode, String majorName) {
        // 升学学生数量
        List<EmploymentDestinations> employDestinations =  majorSourceService.employmentDestinations(year,schoolCode,majorName);
        // 毕业学生数量
        List<EmploymentNumber> graduateNumbers = majorSourceService.getGraduateNumbers(year, schoolCode, majorName);

        if (employDestinations.isEmpty() || graduateNumbers.isEmpty()) {
            for (int i = 0; i < schoolCode.size(); i++) {
                result[i].put("jyl", "-");
            }
            return result;
        }
        // 创建一个空的 Map 用于存储结果
        Map<String, Double> employDestination = new HashMap<>();

        // 将 ListA 中的数据存入一个 Map 中，键为学校名称，值为 count
        Map<String, Double> mapA = new HashMap<>();
        for (EmploymentDestinations ed : employDestinations) {
            mapA.put(ed.getSchoolName(), ed.getHbProvinceCount());
        }
        // 将 ListB 中的数据存入一个 Map 中，键为学校名称，值为 count
        Map<String, Double> mapB = new HashMap<>();
        for (EmploymentNumber gn : graduateNumbers) {
            mapB.put(gn.getSchoolName(), gn.getCount());
        }

        // 合并两个 Map 的键集，得到所有学校名称的集合
        Set<String> allSchool = new HashSet<>();
        allSchool.addAll(mapA.keySet());
        allSchool.addAll(mapB.keySet());
        // 遍历所有学校名称
        for (String schoolName : allSchool) {
            double countTea = mapA.getOrDefault(schoolName, 0.0);
            double countStu = mapB.getOrDefault(schoolName, 0.0);
            // 避免除零错误，将结果设为 0
            double ratio = countStu == 0 ? 0 : countTea / countStu;
            employDestination.put(schoolName, ratio);
        }

        // 归一化处理
        double minScore = Collections.min(employDestination.values());
        double maxScore = Collections.max(employDestination.values());
        if(maxScore == minScore){
            for (int i = 0; i < schoolCode.size(); i++) {
                result[i].put("jyqx", "1");
            }
            return result;
        }else {
            for (Map.Entry<String, Double> entry : employDestination.entrySet()) {
                double score = Math.round((entry.getValue() - minScore) / (maxScore - minScore) * 10000.0) / 10000.0;
                employDestination.put(entry.getKey(), score);
            }
        }
        result = jsonPut(result, schoolCode, employDestination, "jyqx");
        return result;
    }












    // 毕业论文抽检合格率
    public JSONObject[] passRate(JSONObject[] result, String year, List<String> schoolCode, String majorName) {
        for(int i = 0; i < schoolCode.size(); i++){
            result[i].put("lwcjhgl", "督导处");
        }
        return result;
    }








    //就业与专业相关度
    public JSONObject[] professionalRelevance(JSONObject[] result, String year, List<String> schoolCode, String majorName) {
        for(int i = 0; i < schoolCode.size(); i++){
            result[i].put("jyyzyxgd", "问卷数据");
        }
        return result;
    }


    public JSONObject[] jsonPut(JSONObject[] result , List<String> schoolCode,Map<String,Double> map,String target){
        for (int i = 0; i < schoolCode.size(); i++) {
            String schoolName = (String) result[i].get("school");
            if (map.containsKey(schoolName)) {
                double score = map.get(schoolName);
                score = Math.round(score * 10000.0) / 10000.0;
                result[i].put(target, score);
            } else {
                result[i].put(target, "-");
            }
        }
        return result;
    }
}